package se392.ateam2006.meetingmgmt.meeting;

import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Entity;
import javax.persistence.Id;

import se392.ateam2006.exceptions.InvalidValueException;

/**
 * Entity class to store the attributes and operations of a Duration (days, mins, secs)
 * This information can then be persisted to the database
 * @author Ateam (Matthew Bennett, Claire Melton, Shingai Manyiwa, John Adderley)
 * @version 25/03/07
 */
@Entity
public class Duration implements Serializable{
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    
    private int days = 0;
    private int hours = 0;
    private int mins = 0;
    
    /**
     * Default constructor required as this is an entity class
     */
    public Duration() {}
    
    /**
     * Creates a new instance of Duration
     * @param days - the number of days
     * @param hours - the number of hours
     * @param mins - the number of minutes
     * @throws InvalidValueException
     */
    public Duration(int days, int hours, int mins) throws InvalidValueException {

        setDays(days);
        setHours(hours);
        setMins(mins);
    }
    
    /**
     * Get the ID of the duration
     * @return long - the id
     */
    public long getID() {
        return id;
    }

    /**
     * Get the number of days in the duration
     * @return int - number of days
     */
    public int getDays() {
        return days;
    }

    /**
     * Set the number of days
     * @param days - the number of days
     */
    public void setDays(int days) {
        this.days = days;
    }

    /**
     * Get the number of hours in the duration
     * @return int - number of hours
     */
    public int getHours() {
        return hours;
    }

    /**
     * Set the number of hours
     * @param days - the number of hours
     * @throws InvalidValueException
     */
    public void setHours(int hours) throws InvalidValueException {
        if(hours >=0 && hours <= 23){
            this.hours = hours;
        }else{
            throw new InvalidValueException("Hours must be in the range 0 - 23");
        }
    }

    /**
     * Get the number of minutes in the duration
     * @return int - number of minutes
     */
    public int getMins() {
        return mins;
    }

    /**
     * Set the number of minutes
     * @param days - the number of minutes
     * @throws InvalidValueException
     */
    public void setMins(int mins) throws InvalidValueException {
        if(mins >=0 && mins <= 59){
            this.mins = mins;
        }else{
            throw new InvalidValueException("Minutes must be in the range 0 - 59");
        }        
    }
    
    /**
     * Get the length of the duration in milliseconds
     * @return long - the length in miliseconds
     */
    public long getTimeInMillis(){
        return  (((hours * 60) + (days * 24 * 60) + mins)  * 60 * 1000);
    }
    
    public int getTimeInMinutes(){
        return (((int)getTimeInMillis() / 1000) / 60);
    }
    
    /**
     * Override the toString method...
     * @return String - string representation of our duration
     */
    public String toString(){
        
        if(mins > 9){        
            return days + " " + hours + ":" + mins;
        }else{
            return days + " " + hours + ":0" + mins;
        }
    }
    
    /**
     * Override the equals method...
     * @return boolean - are the objects equal?
     */
    public boolean equals(Object obj){
        if(obj == null || !(obj instanceof Duration)){
            return false;
        }else{
            Duration other = (Duration)obj;
            
            if(getDays() == other.getDays() &&
               getHours() == other.getHours() &&
               getMins() == other.getMins()){
               return true;
            }else{
                return false;
            }
        }
    }
}
